﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace SuperMarketManagement.DAO.NHibernate
{
    /// <summary>
    /// DAOFactory takes responsibility to instantiate and dispose session, assign session to DAOs
    /// </summary>
    public class DAOFactory : IDisposable
    {
        public ISession Session { get; private set; }
        private readonly bool privateSession;
        private EmployeeDAO employeeDAO;
        private RoleDAO roleDAO;
        private CustomerDAO customerDAO;
        private CategoryDAO categoryDAO;
        private ProductDAO productDAO;
        private SaleProgramDAO saleProgramDAO;
        private ProductBlockDAO productBlockDAO;
        private InvoiceDAO invoiceDAO;

        public DAOFactory()
            : this(SessionProvider.OpenSession())
        {
            privateSession = true;
        }
        public DAOFactory(ISession session)
        {
            Session = session;
            employeeDAO = new EmployeeDAO(session);
            roleDAO = new RoleDAO(session);
            customerDAO = new CustomerDAO(session);
            categoryDAO = new CategoryDAO(session);
            productDAO = new ProductDAO(session);
            saleProgramDAO = new SaleProgramDAO(session);
            productBlockDAO = new ProductBlockDAO(session);
            invoiceDAO = new InvoiceDAO(session);
        }

        public EmployeeDAO EmployeeDAO
        {
            get { return employeeDAO; }
        }

        public RoleDAO RoleDAO
        {
            get { return roleDAO; }
        }

        public CustomerDAO CustomerDAO
        {
            get { return customerDAO; }
        }

        public CategoryDAO CategoryDAO
        {
            get { return categoryDAO; }
        }

        public ProductDAO ProductDAO
        {
            get { return productDAO; }
        }

        public SaleProgramDAO SaleProgramDAO
        {
            get { return saleProgramDAO; }
        }

        public ProductBlockDAO ProductBlockDAO
        {
            get { return productBlockDAO; }
        }

        public InvoiceDAO InvoiceDAO
        {
            get { return invoiceDAO; }
        }

        public void Dispose()
        {
            if (privateSession)
            {
                Session.Dispose();
            }
            Session = null;
        }
    }
}